package com.mallcai.bigdata.ladon.dpl.vo;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author feixy
 * @version 1.0
 * @date 2019-07-09 11:26
 */
public enum AggFunction {

    MAX,
    MIN,
    AVG,
    SUM,
    COUNT_DISTINCT{
        @Override
        public String toSql(String field) {
            StringBuilder ans = new StringBuilder();
            ans.append("COUNT").append("( DISTINCT ").append(field).append(" )").append(" AS ").append(field);
            return ans.toString();
        }
    },
    COUNT;



    private static final Map<String, AggFunction> CACHED_MAP = new HashMap();

    static {
       Arrays.stream(AggFunction.values()).forEach(aggFunction -> CACHED_MAP.put(aggFunction.getCode(),aggFunction));
    }

    public String toSql(String field){
        StringBuilder ans = new StringBuilder();
        ans.append(this.name()).append("( ").append(field).append(" )").append(" AS ").append(field);
        return ans.toString();
    }

    public static AggFunction getByCode(String code){
        return CACHED_MAP.get(code.toLowerCase());
    }

    public String getCode(){
        return this.name().replace("_"," ").toLowerCase();
    }


}
